package com.fosdata.dpm.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class ApiInterceptor extends WebMvcConfigurationSupport {


    @Autowired
    private AuthInterceptor authInterceptor;


    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/**/user/login","/user/captcha","/web-app/scanTheCode");

    }

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 对所有接口生效
                .allowedOrigins("*")  // 允许的来源（Spring Boot 2.4+ 推荐用 allowedOriginPatterns，旧版本用 allowedOrigins）
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")  // 允许的请求方法
                .allowedHeaders("*")  // 允许的请求头
                .allowCredentials(true)  // 是否允许携带 cookie（如果前端需要跨域传递 cookie，这里必须为 true，且 allowedOriginPatterns 不能用 "*"，需指定具体域名）
                .maxAge(3600);  // 预检请求的有效期（秒），有效期内无需重复预检
    }
}
